﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class PeopleDatesUC : UserControl
    {
        #region Properties / Fields
        private int PeoplePK { get; set; }
        private AddUpdateMode _mode = AddUpdateMode.Neither;
        #endregion Properties / Fields

        #region Constructor & Load
        public PeopleDatesUC()
        {
            InitializeComponent();
        }

        private void PeopleDatesUC_Load(object sender, EventArgs e)
        {

        }

        #endregion Constructor & Load

        #region Populate Methods
        public void LoadPersonInfo(int peoplePK)
        {
            this.PeoplePK = peoplePK;
            this.RefreshGrid();
            if (UserInformation.ViewOnlyInd) this.SetReadOnly();
        }

        private void PopulateDateTypeCombo()
        {
            cboDateType.DataSource = null;

            DataTable dt = new DataTable();
            dt.Columns.Add("LKDateTypeFK", typeof(System.Int32));
            dt.Columns.Add("DescriptionText", typeof(System.String));

            // Get all the records from the database, new and deleted
            LKDateTypeBO dtBO = new LKDateTypeBO();
            IEnumerable<LKDateType> drsDT = dtBO.GetRecords();

            // Grandfather any types that they currently have
            DataTable dtExisting = (DataTable)grdDates.DataSource;

            foreach (LKDateType drDT in drsDT)
            {
                bool currentlyHave = dtExisting.Select("LKDateTypeFK = " + drDT.LKDateTypePK).Length > 0;

                if (drDT.ActiveInd == true || currentlyHave)
                    dt.Rows.Add(new object[] { drDT.LKDateTypePK, drDT.DescriptionText });
            }

            cboDateType.DataSource = dt;
            cboDateType.DisplayMember = "DescriptionText";
            cboDateType.ValueMember = "LKDateTypeFK";
        }

        private void RefreshGrid()
        {
            this.grdDates.DataSource = null;

            DataTable dt = new DataTable();
            dt.Columns.Add("ImportantDatePK", typeof(System.Int32));
            dt.Columns.Add("LKDateTypeFK", typeof(System.Int32));
            dt.Columns.Add("DateType", typeof(String));
            dt.Columns.Add("TheDate", typeof(String));
            dt.Columns.Add("Notes", typeof(String));

            ImportantDateBO oID = new ImportantDateBO();
            IEnumerable<ImportantDate> data = oID.GetActiveDatesByPeoplePK(this.PeoplePK);

            foreach (ImportantDate drID in data)
            {
                dt.Rows.Add(new object[] { drID.ImportantDatePK, drID.LKDateTypeFK, drID.LKDateType.DescriptionText, drID.TheDate, drID.Notes });
            }

            this.grdDates.DataSource = dt;
            grdDates.Columns["ImportantDatePK"].Visible = false;
            grdDates.Columns["LKDateTypeFK"].Visible = false;
            grdDates.Columns["Notes"].Visible = false;

            this.PopulateDateTypeCombo();
        }

        private void LoadDetails()
        {
            cboDateType.SelectedValue = (int)grdDates.SelectedRows[0].Cells["LKDateTypeFK"].Value;
            dtpDate.Value = Convert.ToDateTime(grdDates.SelectedRows[0].Cells["TheDate"].Value);
            txtNotes.Text = grdDates.SelectedRows[0].Cells["Notes"].Value.ToString().Trim();
        }

        private void SetReadOnly()
        {
            btnAdd.Enabled = false;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }
        #endregion Populate Methods

        #region Control Events
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cboDateType.SelectedValue == null || (int)cboDateType.SelectedValue < 1)
            {
                MessageBox.Show("You must select a date type");
                return;
            }

            if (dtpDate.Value == null)
            {
                MessageBox.Show("You must select a date");
                return;
            }

            if (this._mode == AddUpdateMode.Add)
                new ImportantDateBO().AddRecord(PeoplePK, (int)cboDateType.SelectedValue,
                    Convert.ToDateTime(dtpDate.Value), txtNotes.Text.Trim());
            else if (this._mode == AddUpdateMode.Update)
                new ImportantDateBO().UpdateRecord((int)grdDates.SelectedRows[0].Cells["ImportantDatePK"].Value, PeoplePK, (int)cboDateType.SelectedValue,
                    Convert.ToDateTime(dtpDate.Value), txtNotes.Text.Trim());
            
            this.txtNotes.Text = "";
            this._mode = AddUpdateMode.Neither;
            this.grpAddEdit.Text = "Add/Update";
            this.grpAddEdit.Enabled = false;
            this.RefreshGrid();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Are you sure?", "Confirm Cancel", MessageBoxButtons.YesNo))
            {
                this.txtNotes.Text = "";
                this._mode = AddUpdateMode.Neither;
                this.grpAddEdit.Text = "Add/Update";
                this.grpAddEdit.Enabled = false;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.grpAddEdit.Enabled = true;
            this.grpAddEdit.Text = "Add";
            this._mode = AddUpdateMode.Add;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (this.grdDates.SelectedRows.Count == 0)
            {
                MessageBox.Show("Please select the record you'd like to update");
                return;
            }

            this.grpAddEdit.Enabled = true;
            this.grpAddEdit.Text = "Update";
            this._mode = AddUpdateMode.Update;
            this.LoadDetails();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.grdDates.SelectedRows.Count == 0)
            {
                MessageBox.Show("Please select the record you'd like to update");
                return;
            }

            if (DialogResult.Yes == MessageBox.Show("Are you sure?", "Confirm Delete", MessageBoxButtons.YesNo))
            {
                int pk = (int)this.grdDates.SelectedRows[0].Cells["ImportantDatePK"].Value;
                new ImportantDateBO().DeleteRecord(pk);
                this.RefreshGrid();
            }
        }

        private void grdDates_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.btnUpdate.PerformClick();
        }

        private void grdDates_SelectionChanged(object sender, EventArgs e)
        {
            if (this.grdDates.SelectedRows.Count > 0)
            {
                this.txtNotesPreview.Text = this.grdDates.SelectedRows[0].Cells["Notes"].Value.ToString();
            }
            else
            {
                this.txtNotesPreview.Text = "";
            }
        }

        private void grpAddEdit_EnabledChanged(object sender, EventArgs e)
        {
            grdDates.Enabled = (grpAddEdit.Enabled == false);
            btnAdd.Enabled = (grpAddEdit.Enabled == false);
            btnUpdate.Enabled = (grpAddEdit.Enabled == false);
            btnDelete.Enabled = (grpAddEdit.Enabled == false);
        }

        #endregion Control Events

        #region Enumeration
        private enum AddUpdateMode { Add, Update, Neither };

        #endregion Enumeration
    }
}
